package tech.summerly.weather.component.view

import android.content.Context
import android.support.v7.widget.CardView
import android.transition.TransitionManager
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.core.content.edit
import androidx.core.view.isGone
import kotlinx.android.synthetic.main.view_hint.view.*
import tech.summerly.weather.AppContext
import tech.summerly.weather.R
import tech.summerly.weather.util.string

class HintView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : CardView(context, attrs, defStyleAttr) {

    private companion object {
        const val ID_CITY_MANAGER_HINT = "ID_CITY_MANAGER_HINT"
    }

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    private var id: String? = null

    init {
        View.inflate(context, R.layout.view_hint, this)
        buttonOk.setOnClickListener {
            hide()
        }
        buttonNeverAsk.setOnClickListener {
            hide(false)
        }
        useCompatPadding = true
        attrs?.also {
            val typedArray = context.obtainStyledAttributes(attrs, R.styleable.HintView)
            val id = typedArray.getString(R.styleable.HintView_hint_id)
            if (id != null && id.isNotEmpty()) {
                setHint(id)
            }
            typedArray.recycle()
        }
    }

    /**
     * @param id see [HintView.Companion]
     */
    fun setHint(id: String) {
        this.id = id
        val hint = Hints[id]
        if (hint == null) {
            hide()
            return
        }
        textTitle.text = hint.title
        textContent.text = hint.content
    }

    private fun hide(showAgain: Boolean = true) {
        if (!showAgain) {
            id?.let(Hints::setNeverAskAgain)
        }
        if (isGone) {
            return
        }
        TransitionManager.beginDelayedTransition(findRootView())
        isGone = true
    }

    private fun findRootView(): ViewGroup {
        var fallback: ViewGroup? = null
        var find: View? = this
        do {
            val temp = find
            if (temp is FrameLayout) {
                if (temp.id == android.R.id.content) {
                    return temp
                } else {
                    fallback = temp
                }
            }
            if (temp != null) {
                val parent = temp.parent
                find = parent as? View
            }
        } while (find != null)

        return fallback ?: this
    }


    private object Hints {

        private const val NAME_PREFERENCE = "hint_preference"

        private val preference = AppContext.instance.getSharedPreferences(NAME_PREFERENCE, Context.MODE_PRIVATE)

        private val hints = listOf(
                Hint(ID_CITY_MANAGER_HINT, "左右滑动元素以删除城市\n长按元素上下拖动以改变顺序\n点击屏幕右下角按钮来添加城市")
        )

        operator fun get(id: String): Hint? {
            return hints.find { it.id == id }?.takeIf { !isNeverAskAgain(it) }
        }


        fun setNeverAskAgain(id: String) {
            preference.edit {
                putBoolean(id, true)
            }
        }

        private fun isNeverAskAgain(hint: Hint): Boolean {
            return preference.getBoolean(hint.id, false)
        }

    }

    private class Hint(
            val id: String,
            val content: String,
            val title: String = string(R.string.title_hint)
    )
}